<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Labor extends Model
{
    use HasFactory;

    protected $fillable = ['user_id','project_id','price','start_date','end_date','person_id','range','type','b_mobile','b_name','file','remarks','address','push_id'];

    const POSTONLY = ['user_id','project_id','price','start_date','end_date','person_id','range','type','b_mobile','b_name','file','remarks','address','push_id'];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }


    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function person()
    {
        return $this->hasOne(User::class,'id','person_id')->select('id','name');
    }

    public function payment_apply()
    {
        return $this->hasOne(PaymentApply::class,'source_id','id')->select('id','apply_date','status','review_comment','source_id','source_type')->where('source_type','labor');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','Labor')->orderBy('id','desc');
    }

    public function applicationPay()
    {
        return $this->hasMany(Application::class,'source_id','id')->where('model','LaborPay')->where('status','approved')->orderBy('id','desc');
    }
    
    public function contract()
    {
        return $this->hasOne(LaborContract::class,'labor_id','id')->with('application')->select('id','b_name','labor_id','tax_rate');
    }
}
